import csv


# 打开并读取CSV文件
def read_csv_file(file_path):
    with open(file_path, 'r') as file:
        reader = csv.reader(file)


        data = list(reader)
    return data


# 从数据中提取数学分数
def extract_math_scores(data):
    scores = []


    for row in data[1:]:  # 跳过表头
        scores.append(float(row[2]))  # 假设数学分数在第三列
    return scores


# 计算平均值
def calc_mean(scores):
    return sum(scores) / len(scores)
# 计算中位数
def calc_median(scores):
    sorted_scores = sorted(scores)
    n = len(sorted_scores)
    if n % 2 == 1:
       return sorted_scores[n // 2]
    else:
       return (sorted_scores[n // 2 - 1] + sorted_scores[n // 2]) / 2


# 计算众数
def calc_mode(scores):
    score_count = {}


    for score in scores:
        score_count[score] = score_count.get(score, 0) + 1
    max_count = max(score_count.values())
    mode = [k for k, v in score_count.items() if v == max_count]
    return mode[0] if mode else None

# 主程序
file_path = 'scores.csv'  # 假设文件名为scores.csv，可根据实际情况修改
data = read_csv_file(file_path)
math_scores = extract_math_scores(data)
mean = calc_mean(math_scores)
median = calc_median(math_scores)
mode = calc_mode(math_scores)

print(f"The mean is {mean}")
print(f"The median is {median}")
print(f"The mode is {mode}")

